﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using YouYi.YingShengBack.IRepository;

namespace YouYi.YingShengBack.Repository
{
    public class BaseRepository<T, TS> : IBaseRepository<T, TS> where T : class, new() where TS : DbContext, new()
    {
        private DbContext dbContext = DbCpntextFactory<TS>.GetDBContext();
        /// <summary>
        /// 通用单表添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(T entity)
        {
            dbContext.Set<T>().Add(entity);
            return dbContext.SaveChanges() > 0;
        }
        /// <summary>
        /// 通用单表修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            dbContext.Entry<T>(entity).State = EntityState.Modified;
            return dbContext.SaveChanges() > 0;
        }
        /// <summary>
        /// 通用单表删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DelToClass(T entity)
        {
            dbContext.Entry<T>(entity).State = EntityState.Deleted;
            return dbContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 通用单表批量删除
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public bool DelToList(List<T> entitys)
        {
            dbContext.Set<T>().RemoveRange(entitys);
            return dbContext.SaveChanges() > 0;
        }

        /// <summary>
        /// 通用id 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DelToArray(params int[] ids)
        {
            foreach (var item in ids)
            {
                var t = dbContext.Set<T>().Find(item);
                dbContext.Set<T>().Remove(t);
            }
            return dbContext.SaveChanges() > 0;
        }
        /// <summary>
        /// 返回一个表数据集合
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public List<T> SetList(Expression<Func<T, bool>> expression)
        {
            return dbContext.Set<T>().Where(expression).ToList();
        }
        /// <summary>
        /// 返回一个记录
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public T Set(Expression<Func<T, bool>> expression)
        {
            return dbContext.Set<T>().Where(expression).SingleOrDefault();
        }
        /// <summary>
        /// 判断记录是否存在
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public bool Exist(Expression<Func<T, bool>> expression)
        {
            return dbContext.Set<T>().Where(expression).Any();
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="where"></param>
        /// <param name="orderBy"></param>
        /// <param name="count"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public List<T> SetPageList<TY>(int page, int limit, Expression<Func<T, bool>> where, Expression<Func<T, TY>> orderBy, out int count, bool isAsc = true)
        {
            count = dbContext.Set<T>().Where(where).Count();
            if (!isAsc)
            {
                return dbContext.Set<T>().Where(where)
                    .OrderByDescending(orderBy).
                    Skip((page - 1) * limit).
                    Take(limit).ToList();
            }
            return dbContext.Set<T>().Where(where)
                    .OrderBy(orderBy).
                    Skip((page - 1) * limit).
                    Take(limit).ToList();
        }
        /// <summary>
        /// 返回总数
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public int Count(Expression<Func<T, bool>> where)
        {
            return dbContext.Set<T>().Where(where).Count();
        }
    }
}
